<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\model\Adverts;
use App\model\Chain;

class FrontManageController extends Controller
{
	protected $advert_path = array(
    					'1' => '顶部广告位',
    					'2' => '综合资讯1',
    					'3' => '综合资讯2',
    					'4' => '综合资讯3',
    					'5' => '综合资讯子页',
    					'6' => '增值服务页',
                        '7' => '寻人联盟',
    					);

    public function adverts(Request $request)
    {
    	$label = '广告位管理';
    	$advert_path = $this->advert_path;

		if($request->ajax())
		{
            //后台用户列表接口
            $page = $request->page ? $request->page : 1;
            $limit = $request->limit ? $request->limit : 10;
            $offset = ($page - 1) * $limit;
            $count = Adverts::count('id');
    
            $Advertmsg = Adverts::limit($limit)->offset($offset)->get();

            if (!empty($Advertmsg)) 
            {
                foreach ($Advertmsg as $key => &$value) 
                {
                    $value['pathname'] = $advert_path[$value['paths']];
                    $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                    unset($value);
                }
            }
            return $this->return_ajax_data($Advertmsg,'','',$count);
		}
		return view('admin.frontManage.advertlist',compact('label','advert_path'));
    }

    //广告位添加
    public function advertAction(Request $request)
    {
        $data = $request->all();
        $is_show = !empty($data['is_show']) ? $data['is_show'] : 0;
        $create_time = time();

        $save_advert = new Adverts;
        $save_advert->paths = $data['advert_path'];
        $save_advert->titles = $data['titles'];
        $save_advert->urls = $data['urls'];
        $save_advert->pictures = $data['imgurl'];
        $save_advert->is_show = $is_show;
        $save_advert->create_time = $create_time;
        if($save_advert->save())
        {
            return $this->call_back_status($save_advert);
        }
    }

    public function advertUpdate(Request $request)
    {
        $data = $request->all();    
        $update_data = [
                        'paths'=>$data['advert_path'],
                        'titles'=>$data['titles'],
                        'urls'=>$data['urls'],
                        'pictures'=>$data['imgurl']
                        ];
        $update_status = Adverts::where('id','=',$data['id'])->update($update_data);
        return $this->call_back_status($update_status);
    }

    public function isShow(Request $request)
    {
        $data = $request->all();
        $status = Adverts::where('id','=',$data['id'])->update(['is_show'=>$data['is_show']]);
        return $this->call_back_status($status);
    }

    public function advertdel(Request $request)
    {
        $id = $request->id;
        $status = Adverts::where('id','=',$id)->delete();
        return $this->call_back_status($status);
    }


    public function friendChain(Request $request)
    {
        $label = '友链管理';
        if ($request->ajax()) 
        {
            //后台用户列表接口
            $page = $request->page ? $request->page : 1;
            $limit = $request->limit ? $request->limit : 10;
            $offset = ($page - 1) * $limit;
            $count = Chain::count('id');
    
            $Chainmsg = Chain::limit($limit)->offset($offset)->get();

            if (!empty($Chainmsg)) 
            {
                foreach ($Chainmsg as $key => &$value) 
                {
                    $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                    unset($value);
                }
            }
            return $this->return_ajax_data($Chainmsg,'','',$count);
        }
        return view('admin.frontManage.friendchain',compact('label'));
    }

    //友链添加
    public function addChain(Request $request)
    {
        $data = $request->all();
        $create_time = time();

        $save_chain = new Chain;
        $save_chain->titles = $data['titles'];
        $save_chain->urls = $data['urls'];
        $save_chain->create_time = $create_time;
        if($save_chain->save())
        {
            return $this->call_back_status($save_chain);
        }
    }

    public function editChain(Request $request)
    {
        $data = $request->all();   
        $update_data = [
                        'titles'=>$data['titles'],
                        'urls'=>$data['urls'],
                        ];
        $update_status = Chain::where('id','=',$data['id'])->update($update_data);
        return $this->call_back_status($update_status);
    }

    //删除
    public function chainDel(Request $request)
    {
        $id = $request->id;
        $status = Chain::where('id','=',$id)->delete();
        return $this->call_back_status($status);
    }
}
